package top.soulblack.purchasesale.controller.stock;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import top.soulblack.purchasesale.common.CodeMsg;
import top.soulblack.purchasesale.common.Result;
import top.soulblack.purchasesale.common.enums.OrderStatusEnum;
import top.soulblack.purchasesale.converter.OrderDTOToOrderVO;
import top.soulblack.purchasesale.exception.PurchaseException;
import top.soulblack.purchasesale.model.DTO.GoodDTO;
import top.soulblack.purchasesale.model.DTO.OrderDTO;
import top.soulblack.purchasesale.model.VO.OrderVO;
import top.soulblack.purchasesale.model.form.OrderForm;
import top.soulblack.purchasesale.service.InOutOrderService;
import top.soulblack.purchasesale.service.GoodsService;
import top.soulblack.purchasesale.service.OrderDetailService;
import top.soulblack.purchasesale.service.OrderMasterService;
import top.soulblack.purchasesale.service.RequisitionNoteService;

import java.util.ArrayList;
import java.util.List;


/**
 * @Author: 韦溜溜
 * @Description:
 * @Date: Create in 16:51 2019/7/3
 */
@Controller
@RequestMapping("/requisition_note")
public class RequisitionNoteController {

    @Autowired
    private RequisitionNoteService requisitionNoteService;

    @Autowired
    private OrderMasterService orderMasterService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private InOutOrderService inOutOrderService;

    @Autowired
    private GoodsService goodsService;

    @GetMapping("/getGoodsInfo")
    @ResponseBody
    public Result<GoodDTO> getGoodsInfo(String barCode) {
        return Result.success(requisitionNoteService.getGoodCartesian(barCode));
    }

    @PostMapping("/createNote")
    @ResponseBody
    public Result<List<OrderVO>>createNote(@RequestBody OrderForm orderForm){
        List<OrderDTO> orderDTOList;
        try {
            orderDTOList = requisitionNoteService.createRequisitionNote(orderForm);
        }catch (PurchaseException e){
            return Result.error(new CodeMsg(e.getCode(),e.getMessage()));
        }
        return Result.success(OrderDTOToOrderVO.converter(orderDTOList));
    }

    @PostMapping("/submitForm")
    @ResponseBody
    public Result<List<OrderVO>> determine(@RequestBody List<OrderVO> orderVO){
        orderVO.forEach(e-> {
            e.getOrderMaster().setOrderStatus(OrderStatusEnum.DETERMINATION.getCode());
                }
        );
       try{
           requisitionNoteService.insertFromVO(orderVO);
           return Result.success(orderVO);
       }catch (PurchaseException e){
           return Result.error(new CodeMsg(e.getCode(),e.getMessage()));
       }
    }

    @PostMapping("/draft")
    @ResponseBody
    public Result<List<OrderVO>> draft(@RequestBody List<OrderVO> orderVO){
        try{
            requisitionNoteService.insertFromVO(orderVO);
            return Result.success(orderVO);
        }catch (PurchaseException e){
            return Result.error(new CodeMsg(e.getCode(),e.getMessage()));
        }
    }

    @PostMapping("/getOneOrder")
    @ResponseBody
    public Result<List<OrderVO>> getOneOrder(String orderId){
        List<OrderDTO> orderDTOList = new ArrayList<>();
        try{
           OrderDTO orderDTO= requisitionNoteService.getOrderByOrderId(orderId);
           orderDTOList.add(orderDTO);
         }catch (PurchaseException e){
           return Result.error(new CodeMsg(e.getCode(),e.getMessage()));
         }
        return Result.success(OrderDTOToOrderVO.converter(orderDTOList));
    }

    @PostMapping("/finish")
    @ResponseBody
    public Result<String> finish(String orderId){
        //TODO 后面在加上权限判断，判断用户使用拥有权力操作这个服务和这个店面
        try{
            orderMasterService.updateStatusByOrderId(orderId,OrderStatusEnum.SUCCESS.getCode());
            return Result.success("调拨成功！");
        }catch (PurchaseException e){
            return Result.error(new CodeMsg(e.getCode(),e.getMessage()));
        }
    }

    @PostMapping("/determine")
    @ResponseBody
    public Result<String> determine(String orderId ,String remarks){
        //TODO 后面在加上权限判断，判断用户使用拥有权力操作这个服务和这个店面
        try{
            orderMasterService.updateStatusByOrderId(orderId,OrderStatusEnum.TRANSFERRING.getCode());
            orderMasterService.updateRemarksByOrderId(orderId,remarks);
            inOutOrderService.createOutOrder(orderId);
            return Result.success("正在调拨中！");
        }catch (PurchaseException e){
            return Result.error(new CodeMsg(e.getCode(),e.getMessage()));
        }
    }

    @PostMapping("/purchase")
    @ResponseBody
    public Result<List<OrderVO>> purchase(String orderId){
        List<OrderDTO> orderDTOList = new ArrayList<>();
        try{
            OrderDTO orderDTO= requisitionNoteService.getOrderByOrderId(orderId);
            orderDTOList.add(orderDTO);
        }catch (PurchaseException e){
            return Result.error(new CodeMsg(e.getCode(),e.getMessage()));
        }
        return Result.success(OrderDTOToOrderVO.converter(orderDTOList));
    }

    @PostMapping("/close")
    @ResponseBody
    public Result<String> close(String orderId){
        //TODO 后面在加上权限判断，判断用户使用拥有权力操作这个服务和这个店面
        try{
            orderMasterService.updateStatusByOrderId(orderId,OrderStatusEnum.FAIL.getCode());
            return Result.success("正在调拨中！");
        }catch (PurchaseException e){
            return Result.error(new CodeMsg(e.getCode(),e.getMessage()));
        }
    }

}